package com.atguigu.edu.controller;


import com.atguigu.commonUtils.R;
import com.atguigu.edu.entity.Chapter;
import com.atguigu.edu.entity.chapter.ChapterVo;
import com.atguigu.edu.service.ChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Rayyy666
 * @since 2022-04-02
 */
@RestController
@RequestMapping("/edu/chapter")
//@CrossOrigin //解决跨域问题
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    /**
     * 查询课程大纲列表，根据课程id进行查询
     *
     * @return
     */
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId) {

        List<ChapterVo> list = chapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("allChapterVideo", list);
    }

    /**
     * 添加章节
     *
     * @param chapter
     * @return
     */
    @PostMapping("addChapter")
    public R addChapter(@RequestBody Chapter chapter) {

        chapterService.save(chapter);
        return R.ok();
    }

    /**
     * 根据ID查询章节
     *
     * @param chapterId
     * @return
     */
    @GetMapping("getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId) {
        Chapter chapter = chapterService.getById(chapterId);
            return R.ok().data("chapter", chapter);
    }

    /**
     * 修改章节信息
     *
     * @param chapter
     * @return
     */
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody Chapter chapter) {
        chapterService.updateById(chapter);
        return R.ok();
    }

    /**
     * 删除章节
     * @param chapterId
     * @return
     */
    @DeleteMapping("deleteChapter/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId) {
       boolean flag =  chapterService.deleteChapter(chapterId);

       if(flag){
           return R.ok();
       }else{
           return R.error();
        }

    }
}

